package com.roc.template.mongo.base;

import com.mongodb.client.result.UpdateResult;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.lang.reflect.ParameterizedType;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Repository
public  class BaseDaoImpl<T> implements BaseDao<T> {

    @Autowired
    protected MongoTemplate mongoTemplate;

    protected Class<T> getEntityClass() {
        return (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }

    protected Criteria baseCriteria(){
        return Criteria.where("isDelete").is(false);
    }

    @Override
    public T insert(T t) {
        return mongoTemplate.insert(t);
    }

    @Override
    public T save(T t) {
        return mongoTemplate.save(t);
    }

    @Override
    public int batchInsert(List<T> t) {
        Collection<T> ts = mongoTemplate.insertAll(t);
        return ts.size();
    }

    @Override
    public long update(String id,T entity) {
        UpdateResult updateResult = mongoTemplate.updateFirst(Query.query(Criteria.where("_id").is(new ObjectId(id)).and("isDelete").is(false)), update(entity), getEntityClass());
        return updateResult.getModifiedCount();
    }
    private Update update(T entity){
        Update update=new Update();
        Arrays.asList(entity.getClass().getDeclaredFields()).stream().filter(x->!(x.getName().equals("id")||x.getName().equals("serialVersionUID"))).forEach(x->{
            x.setAccessible(true);
            try {
                update.set(x.getName(),x.get(entity));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        });
        update.set("updatedTime",new Date());
        return update;
    }

    @Override
    public T queryById(String id) {
        Query query=new Query();
        query.addCriteria(Criteria.where("_id").is(id).and("isDelete").is(false));
        return mongoTemplate.findOne(query,getEntityClass());
    }

    @Override
    public long delete(String id) {
        Query query=new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update=new Update();
        update.set("isDelete",true);
        UpdateResult updateResult=mongoTemplate.updateFirst(query,update,getEntityClass());
        return updateResult.getModifiedCount();
    }

    @Override
    public long batchDelete(List<String> ids) {
        Query query=new Query();
        query.addCriteria(Criteria.where("_id").in(ids));
        Update update=new Update();
        update.set("isDelete",true);
        UpdateResult updateResult=mongoTemplate.updateMulti(query,update,getEntityClass());
        return updateResult.getModifiedCount();
    }

    @Override
    public  List<T> queryAll() {
        Query query=new Query();
        query.addCriteria(Criteria.where("isDelete").is(false));
        return mongoTemplate.find(query,getEntityClass());
    }

    protected Criteria baseQueryCondition(){
        return Criteria.where("isDelete").is(false);
    }
}
